# # coding = utf-8
# # Author = Ann
# # Date: 2023/6/27
# import os
# import time
# import json
# import xml.etree.ElementTree as ET
# import re
#
# import urllib.request
#
# import requests
# from bs4 import BeautifulSoup
#
# from tools.reader.config_reader import ConfigReader
#
#
# class UpdateEdge:
#
#     def __init__(self):
#         self.directory_path = ConfigReader().read("driver")["edge_driver_path"]
#         self.file_dir = None
#         self.driver_path = None
#         self.edge_version = None
#         self.list_version_all = []
#         self.url = \
#             "https://artifactory.global.standardchartered.com/artifactory/generic-genie-release/selenium-drivers/edge/"
#
#     def get_version(self):
#         tree = ET.parse(r'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.VisualElementsManifest.xml')
#         root = tree.getroot()
#
#         for elem in root.iter('VisualElements'):
#             ve_text = ET.tostring(elem, encoding='unicode', method='xml')
#             rematch = re.match(r'(.*)\"(.*)\\VisualElements\\Logo.png', ve_text)
#             self.edge_version = rematch.group(2)
#             return self.edge_version
#
#     def find_version(self):
#         if ConfigReader().read("project")["driver"].lower() == "edge":
#             response = urllib.request.urlopen(self.url)
#             content = response.read().decode('utf-8')
#             soup = BeautifulSoup(content, 'html.parser')
#             name_list = soup.select('pre a')
#             for name in name_list:
#                 text_version = name.get_text().strip('/')
#                 if re.search(r'^\d', text_version) and len(text_version) >= 10:
#                     self.list_version_all.append(text_version)
#         return self.list_version_all
#
#     def download_driver(self):
#         self.edge_version = self.get_version()
#         self.driver_path = f"{self.directory_path}/{self.edge_version}"
#         self.file_dir = f"{self.driver_path}/msedgedriver.exe"
#         self.list_version_all = self.find_version()
#
#         if self.edge_version in self.list_version_all:
#             self.download_specific_version(self.edge_version)
#         else:
#             self.find_and_download_closest_version()
#
#     def download_specific_version(self, version):
#         version_url = self.url + version + "/windows/msedgedriver_x64.exe"
#         response = requests.get(url=version_url)
#         open(self.file_dir, "wb").write(response.content)
#
#     def find_and_download_closest_version(self):
#         list_min = []
#         major_version = self.edge_version[:-3]
#         for major in self.list_version_all:
#             if re.search(f"^{major_version}", major):
#                 list_min.append(int(major[-2:]))
#                 list_min.sort()
#         match_edge_version = major_version + "." + str(list_min[-1])
#         self.edge_version = match_edge_version
#         self.download_specific_version(match_edge_version)
#
#     def update_edge(self):
#         self.edge_version = self.get_version()
#         self.driver_path = f"{self.directory_path}/{self.edge_version}"
#         self.file_dir = f"{self.driver_path}/msedgedriver.exe"
#         is_exist = os.path.exists(self.driver_path)
#         version_json = f"{self.directory_path}/version.json"
#         if not is_exist:
#             os.makedirs(self.driver_path)
#         if os.path.isfile(self.driver_path):
#             try:
#                 with open(version_json, "r+") as file:
#                     data = json.load(file)
#                     for i in data:
#                         if i["version"] == self.edge_version:
#                             pass
#                         else:
#                             now_time = time.strftime("%Y.%m.%d.%H.%M.%S")
#                             data[0]["lastUpdate"] = now_time
#                             json.dump(data, file)
#             except (IOError, json.JSONDecodeError) as e:
#                 print(f"Failed to update version file: {e}")
#         else:
#             try:
#                 now_time = time.strftime("%Y.%m.%d.%H.%M.%S")
#                 data = [{"source": self.url, "version": self.edge_version,
#                          "path": os.path.abspath(self.file_dir), "lastUpdate": now_time}]
#                 with open(version_json, "w") as file:
#                     json.dump(data, file)
#                 file.close()
#                 self.download_driver()
#             except IOError as e:
#                 print(f"Failed to create version file: {e}")
